var app = getApp();
Component({
  properties: {
    DataInfo: {
      type: null
    },
    store: {
      type: null
    },
    store_id: {
      type: null
    },
    goods_id: {
      type: Number
    },
    isType: {
      type: null
    }
  },
  data: {
    imgPath: app.globalData.imgPath,
    // store: '',
    uploadUrl: '',
    store_id: '',
    sectionOptions: [],
    objectMultiArray: [],
    multiIndex: [0, 0, 0],
    regional: '',
    cargon_data: {
      title: '',
      sku: '',
      thumb: '',
      price: '',
      market_price: '',
      cost_price: '',
      stock: '',
      thumb_url: '',
      goods_sn:"",
      product_sn:""
    },
    imageUrl: '',
    fileList2: [],
    fileList1: [],
    upNumMax: 0,
    upNumMaxOther: 0,
    fileList3: [],
    fileList4: [],
    selectedOptions3: [],
    jumpState: true,
    shopText: '',//商品详情文字

    show:false,
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() { },
    moved() { },
    detached() { },
    ready() {
      console.log(this.properties.store);
      if (this.properties.store) {
        this.setData({
          store: this.data.store
        });
      }
      if (this.properties.store_id) {
        this.setData({
          store_id: this.data.store_id
        });
        this._getJson();
      }
      if (this.properties.goods_id) {
        this.setData({
          goods_id: this.properties.goods_id
        });
        this._getJson(true);

      }
      this._urlUp();
    }
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() { },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() { },
    hide() { },
    resize() { }
  },

  methods: {
    handle_picker(){
      let show = this.data.show;
      this.setData({show:!show});
    },
    onCancel(){
      this.setData({show:false});
    },
    onChange(e){
      let index = e.detail.index;
      if(index==0){
        let name = e.detail.value[0];
        let _result = this.data.sectionOptions.filter(item=>item.name == name);
        if(_result[0].children&&_result[0].children.length!==0){
          let _values_2 = _result[0].children.map(obj=>obj.name);
          this.setData({
            'objectMultiArray[1].values':_values_2
          })
          let three_children = _result[0].children[0].children;
          if(three_children&&three_children!==0){
            let _values_3 = three_children.map(obj=>obj.name);
            this.setData({
              'objectMultiArray[2].values':_values_3
            })
          }else{
            this.setData({
              'objectMultiArray[2].values':[]
            })
          }
        }else{
          this.setData({
            'objectMultiArray[1].values':[],
            'objectMultiArray[2].values':[]
          })
        }
      }else if(index==1){
        let name_1 = e.detail.value[0];
        let name_2 = e.detail.value[1];
        let _result_1 = this.data.sectionOptions.filter(item=>item.name == name_1);
        let _result_2 = _result_1[0].children.filter(item=>item.name == name_2);
        if(_result_2[0].children&&_result_2[0].children.length!==0){
          let _values_2 = _result_2[0].children.map(obj=>obj.name);
          this.setData({
            'objectMultiArray[2].values':_values_2
          })
        }else{
          this.setData({
            'objectMultiArray[2].values':[]
          })
        }
      }

    },
    onConfirm(e){
      let index_1 = e.detail.index[0];
      let index_2 = e.detail.index[1];
      let level_1 = this.data.sectionOptions[index_1].id;
      let level_2 =  this.data.sectionOptions[index_1].children[index_2].id;
      if(e.detail.value[2]){
        let index_3 = e.detail.index[2];
        let level_3 = this.data.sectionOptions[index_1].children[index_2].children[index_3].id;
        this.setData({
          'selectedOptions3[0]':level_1,
          'selectedOptions3[1]':level_2,
          'selectedOptions3[2]':level_3,
          regional:e.detail.value.join('/')
        })
      }else{
        this.setData({
          'selectedOptions3[0]':level_1,
          'selectedOptions3[1]':level_2,
          regional:e.detail.value.join('/')
        })
      }
      this.setData({
        show:false
      })
    },
    input_handle(e){
      let _value = e.detail.value;
      let field = e.currentTarget.dataset.field;
      let revise = 'cargon_data.'+field;
      this.setData({[revise]:_value});
    },
    areaInp(e) {
      console.log(e);
      let txt = e.detail.value;
      this.setData({
        shopText: txt
      });
    },
    // 商品图片上传
    _urlUp() {
      if (this.data.store == 'store') {
        this.setData({
          uploadUrl: 'upload.uploadPic'
        });
      } else if (this.data.store == 'supplier') {
        this.setData({
          uploadUrl: 'upload.uploadPic'
        });
      }
    },
    // 选择分类对接数据
    _getJson(flag = false) {
      let that = this;

      if (that.data.store == 'store') {
        let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.get-category-by-store-id');
        urlStr += '&store_id=' + that.properties.store_id;
        app._getNetWork({
          url: urlStr,
          success: function (resdata) {
            var res = resdata.data;
            let list = [];
            console.log(res,'ssssssss')
            if(res.data.length >0){
              res.data.list.map(item => {
                list.push({
                  name: item.name,
                  id: item.id,
                  childrens: item.childrens
                });
              });
            }


            that.setData({
              list
            });
            if (res.result == 1) {
              let sectionOptionsData = [];

              for (let i = 0; i < res.data.list.length; i++) {
                sectionOptionsData.push({
                  id: res.data.list[i].id,
                  name: res.data.list[i].name,
                  children: []
                });
                for (let k = 0; k < res.data.list[i].childrens.length; k++) {
                  sectionOptionsData[i].children.push({
                    id: res.data.list[i].childrens[k].id,
                    name: res.data.list[i].childrens[k].name
                  });
                }
              }

              that.setData({
                sectionOptions: sectionOptionsData
              });
              console.log(sectionOptionsData);
              that.goodsHandlefunction();
              if (flag) that.getGoodsJson();
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            console.log(res.msg);
          }
        });
      } else if (that.data.store == 'supplier') {
        let urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.get-category');
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: {},
          success: function (resdata) {
            var res = resdata.data;
            let list = [];
            res.data.map(item => {
              list.push({
                name: item.name,
                id: item.id,
                childrens: item.childrens
              });
            });

            that.setData({
              list
            });
            if (res.result == 1) {
              let sectionOptionsData = [];
              for (var i = 0; i < res.data.length; i++) {
                sectionOptionsData.push({
                  id: res.data[i].id,
                  name: res.data[i].name,
                  children: []
                });
                if (!res.data[i].childrens || res.data[i].childrens.length < 1) {
                  // 判断是否有二级分类
                } else {
                  // that.sectionOptions[i].children = []
                  for (let k = 0; k < res.data[i].childrens.length; k++) {
                    sectionOptionsData[i].children.push({
                      id: res.data[i].childrens[k].id,
                      name: res.data[i].childrens[k].name,
                      children: []
                    });
                    if (!res.data[i].childrens[k].childrens || res.data[i].childrens[k].childrens.length < 1) {
                      // 判断是否有三级分类
                    } else {
                      for (var j = 0; j < res.data[i].childrens[k].childrens.length; j++) {
                        sectionOptionsData[i].children[k].children.push({
                          id: res.data[i].childrens[k].childrens[j].id,
                          name: res.data[i].childrens[k].childrens[j].name
                        });
                      }
                    }
                  }
                }
              }
              that.setData({
                sectionOptions: sectionOptionsData
              });
              that.goodsHandlefunction();
              if (flag) that.getGoodsJson();
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) { }
        });
      }
    },
    commodityBtn(e) {
      let column = e.detail.column;
      let val = e.detail.value;
      const data = {
        multiArray: this.data.objectMultiArray
      };
      this.setData({
        ['multiIndex[' + column + ']']: val
      });
      switch (column) {
      case 0:
        this.selecommodity_0(val);
        break;
      case 1:
        this.selecommodity_1(this.data.multiIndex[0], val);
        break;
      case 2:
        break;
      }
      this.setData(data);
    },
    goodsHandlefunction() {
      let sectionOptions = this.data.sectionOptions;
      let arr_1 = [];
      let arr_2 = [];
      let arr_3 = [];
      for (let i = 0; i < sectionOptions.length; i++) {
        arr_1.push({
          id: sectionOptions[i].id,
          name: sectionOptions[i].name

        });
      }
      if (sectionOptions[0].children.length > 0) {
        for (let k = 0; k < sectionOptions[0].children.length; k++) {
          arr_2.push({
            id: sectionOptions[0].children[k].id,
            name: sectionOptions[0].children[k].name
          });
        }
      }
      if ((sectionOptions[0].children && sectionOptions[0].children.length > 0) && (sectionOptions[0].children[0].children &&
        sectionOptions[0].children[0].children.length > 0)) {
        for (let j = 0; j < sectionOptions[0].children[0].children.length; j++) {
          arr_3.push({
            id: sectionOptions[0].children[0].children[j].id,
            name: sectionOptions[0].children[0].children[j].name
          });
        }
      }

      let  _values_1 = arr_1.map(obj=>obj.name);
      let  _values_2 = arr_2.map(obj=>obj.name);
      let  _values_3 = arr_3.map(obj=>obj.name);
      console.log(_values_1)
      let _objectMultiArray = [
        {
          values:_values_1
        },
        {
          values:_values_2
        },
        {
          values:_values_3
        }
      ]
      this.setData({
        objectMultiArray:_objectMultiArray
      })
      // this.setData({
      //   'objectMultiArray[0]': arr_1,
      //   'objectMultiArray[1]': arr_2,
      //   'objectMultiArray[2]': arr_3
      // });
      console.log(this.data.objectMultiArray);
    },
    selecommodity_0(index) {
      let sectionOptions = this.data.sectionOptions;
      let arr_2 = [];
      let arr_3 = [];
      if (sectionOptions[index].children.length > 0) {
        for (let k = 0; k < sectionOptions[index].children.length; k++) {
          arr_2.push({
            id: sectionOptions[index].children[k].id,
            name: sectionOptions[index].children[k].name
          });
        }
      }
      if ((sectionOptions[index].children && sectionOptions[index].children.length > 0) && (sectionOptions[index].children[0].children && sectionOptions[index].children[0].children.length > 0)) {
        for (let j = 0; j < sectionOptions[index].children[0].children.length; j++) {
          arr_3.push({
            id: sectionOptions[index].children[0].children[j].id,
            name: sectionOptions[index].children[0].children[j].name
          });
        }
      }
      this.setData({
        'objectMultiArray[1]': arr_2,
        'objectMultiArray[2]': arr_3,
        'multiIndex[1]': 0,
        'multiIndex[2]': 0
      });
    },
    selecommodity_1(index_0, index_1) {
      let sectionOptions = this.data.sectionOptions;
      let arr_3 = [];
      if ((sectionOptions[index_0].children && sectionOptions[index_0].children.length > 0) && (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length > 0)) {
        for (let j = 0; j < sectionOptions[index_0].children[index_1].children.length; j++) {
          arr_3.push({
            id: sectionOptions[index_0].children[index_1].children[j].id,
            name: sectionOptions[index_0].children[index_1].children[j].name
          });
        }
      }
      this.setData({
        'objectMultiArray[2]': arr_3,
        'multiIndex[2]': 0
      });
    },
    commodityconfirm(e) {
      var i = e.detail.value[0];
      if (this.data.list[i]&&this.data.list[i].childrens.length == 0) {
        wx.showToast({
          title: '至少要选择二级分类',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      let sectionOptions = this.data.sectionOptions;
      let indexArray = e.detail.value;
      let index_0 = indexArray[0];
      let index_1 = indexArray[1];
      let index_2 = indexArray[2];
      let idArray = [];
      let strArray = [];
      if (sectionOptions.length > 0) {
        idArray.push(sectionOptions[index_0].id);
        strArray.push(sectionOptions[index_0].name);
        if (sectionOptions[index_0].children.length > 0) {
          idArray.push(sectionOptions[index_0].children[index_1].id);
          strArray.push(sectionOptions[index_0].children[index_1].name);
          if (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length >
            0) {
            idArray.push(sectionOptions[index_0].children[index_1].children[index_2].id);
            strArray.push(sectionOptions[index_0].children[index_1].children[index_2].name);
          }
        }
      }
      this.setData({
        regional: strArray.join('/'),
        selectedOptions3: idArray
      });
    },
    // 选择图片
    onRead() {
      let that = this;
      let urlStr = app.getNetAddresss(this.data.uploadUrl);
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          wx.compressImage({
            src: tempFilePaths[0], // 图片路径
            quality: 60, // 压缩质量
            success(e) {
              let photourl = e.tempFilePath;
              wx.uploadFile({
                url: urlStr,
                filePath: photourl,
                name: 'file',
                success(resdata) {
                  var res = JSON.parse(resdata.data);
                  if (res.result == 1) {
                    that.setData({
                      imageUrl: photourl,
                      'cargon_data.thumb': res.data.img
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: res.msg,
                      duration: 1500
                    });
                  }
                }
              });
            },
            fail(e) { },
            complete(e) { }
          });
        }
      });
    },
    // 选择图片
    onRead_1() {
      // if (this.data.upNumMax == 4 || this.data.upNumMax >= 4) {
      //   wx.showToast({
      //     icon: 'none',
      //     title: '图片数量已达到上限',
      //     duration: 1500
      //   })
      //   return false
      // }
      let that = this;
      let urlStr = app.getNetAddresss(this.data.uploadUrl);
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          wx.compressImage({
            src: tempFilePaths[0], // 图片路径
            quality: 60, // 压缩质量
            success(e) {
              let photourl = e.tempFilePath;
              wx.uploadFile({
                url: urlStr,
                filePath: photourl,
                name: 'file',
                formData: {
                  attach: 'upload'
                },
                success(resdata) {
                  var res = JSON.parse(resdata.data);
                  if (res.result == 1) {
                    let fileList1 = that.data.fileList1;
                    let fileList2 = that.data.fileList2;
                    fileList1.push({
                      url: res.data.img
                    });
                    fileList2.push({
                      url: photourl
                    });
                    that.setData({
                      upNumMax: that.data.upNumMax + 1,
                      fileList1: fileList1,
                      fileList2: fileList2
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: res.msg,
                      duration: 1500
                    });
                  }
                }
              });
            },
            fail(e) { },
            complete(e) { }
          });
        }
      });
    },
    removeImg: function (e) {
      let index = e.currentTarget.dataset.index;
      let fileList1 = this.data.fileList1;
      let fileList2 = this.data.fileList2;
      fileList1.splice(index, 1);
      fileList2.splice(index, 1);
      this.setData({
        fileList1: fileList1,
        fileList2: fileList2
      });
      this.setData({
        upNumMax: this.data.fileList1.length
      });
    },
    // 选择图片
    onRead_2() {
      // if (this.data.upNumMaxOther == 4 || this.data.upNumMaxOther >= 4) {
      //   wx.showToast({
      //     icon: 'none',
      //     title: '图片数量已达到上限',
      //     duration: 1500
      //   })
      //   return false
      // }
      let that = this;
      let urlStr = app.getNetAddresss(this.data.uploadUrl);
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          wx.compressImage({
            src: tempFilePaths[0], // 图片路径
            quality: 60, // 压缩质量
            success(e) {
              let photourl = e.tempFilePath;
              wx.uploadFile({
                url: urlStr,
                filePath: photourl,
                name: 'file',
                formData: {
                  attach: 'upload'
                },
                success(resdata) {
                  var res = JSON.parse(resdata.data);

                  if (res.result == 1) {
                    let fileList3 = that.data.fileList3;

                    let fileList4 = that.data.fileList4;
                    fileList3.push({
                      url: res.data.img_url
                    });
                    fileList4.push({
                      url: photourl
                    });
                    that.setData({
                      upNumMaxOther: that.data.upNumMaxOther + 1,
                      fileList3: fileList3,
                      fileList4: fileList4
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: res.msg,
                      duration: 1500
                    });
                  }
                }
              });
            },
            fail(e) { },
            complete(e) { }
          });
        }
      });
    },
    removeImg_1: function (e) {
      let index = e.currentTarget.dataset.index;
      let fileList3 = this.data.fileList3;
      // let fileList4 = this.data.fileList4
      let infoArr = this.data.infoArr;
      // infoArr.splice(index, 1)

      fileList3.splice(index, 1);
      // fileList4.splice(index, 1)
      this.setData({
        fileList3: fileList3,
        // fileList4: fileList4,
        infoArr
      });
      this.setData({
        upNumMaxOther: this.data.fileList3.length
      });
    },
    // 商品名称值绑定
    titleinp(e) {
      let val = e.detail.value;
      this.setData({
        'cargon_data.title': val
      });
    },
    // 商品单位值绑定
    skuinp(e) {
      let val = e.detail.value;
      this.setData({
        'cargon_data.sku': val
      });
    },
    // 商品现价值绑定
    priceinp(e) {
      let val = e.detail.value;
      if(val.indexOf(".")!==-1){
        let index = val.indexOf(".");
        val =  val.replace(/[^\d]/g,"");
        let newStr = val.slice(0,index) + '.' + val.slice(index);
        this.setData({
          'cargon_data.price': newStr
        });
      }else{
        this.setData({
          'cargon_data.price': val
        });
      }

    },
    // 商品原价值绑定
    marketpriceinp(e) {
      let val = e.detail.value;
      if(val.indexOf(".")!==-1){
        let index = val.indexOf(".");
        val =  val.replace(/[^\d]/g,"");
        let newStr = val.slice(0,index) + '.' + val.slice(index);
        this.setData({
          'cargon_data.market_price': newStr
        });
      }else{
        this.setData({
          'cargon_data.market_price': val
        });
      }
    },
    // 商品成本值绑定
    costPriceinp(e) {
      let val = e.detail.value;
      if(val.indexOf(".")!==-1){
        let index = val.indexOf(".");
        val =  val.replace(/[^\d]/g,"");
        let newStr = val.slice(0,index) + '.' + val.slice(index);
        this.setData({
          'cargon_data.cost_price': newStr
        });
      }else{
        this.setData({
          'cargon_data.cost_price': val
        });
      }

    },
    // 商品库存值绑定
    stockinp(e) {
      let val = e.detail.value;
      this.setData({
        'cargon_data.stock': val
      });
    },
    inpTap(e){
      let key = e.currentTarget.dataset.key;
      this.setData({
        [`cargon_data.${key}`]: e.detail.value,
      })
    },
    //  搞数据
    setJson() {
      var that = this;
      var newArr = [];

      if (that.data.fileList1.length != 0) {
        for (let i = 0; i < that.data.fileList1.length; i++) {
          newArr.push(that.data.fileList1[i].url);
        }
      } else {
        newArr = [];
      }
      var newArr2 = [];
      if (that.data.fileList3.length != 0) {
        for (let i = 0; i < that.data.fileList3.length; i++) {
          newArr2.push(that.data.fileList3[i].url);
        }
      } else {
        newArr2 = [];
      }
      let json = {};
      if (!this.data.isType) {
        if (app._isTextEmpty(this.data.infoArr)) {
          this.setData({
            infoArr: []
          });
        }
        console.log(this.data.infoArr, this.data.fileList3, '两个链接的数组');
        let newInfo = this.data.infoArr.concat(this.data.fileList3);
        newArr2 = [];
        if (newInfo.length != 0) {
          for (let i = 0; i < newInfo.length; i++) {
            if (newInfo[i].url.indexOf('wxfile') == '-1') {
              newArr2.push(newInfo[i].url);
            }
          }
        } else {
          newArr2 = [];
        }
      }

      if (this.data.store == 'store') {
        json = {
          store_id: this.data.store_id,
          category_pid: this.data.selectedOptions3[0],
          category_cid: this.data.selectedOptions3[1],
          title: this.data.cargon_data.title,
          thumb: this.data.cargon_data.thumb,
          thumb_url: newArr2,
          good_content: newArr,
          cost_price: this.data.cargon_data.cost_price,
          market_price: this.data.cargon_data.market_price,
          price: this.data.cargon_data.price,
          sku: this.data.cargon_data.sku,
          stock: this.data.cargon_data.stock,
          goods_sn:this.data.cargon_data.goods_sn,
          product_sn:this.data.cargon_data.product_sn,
        };

        if (!this.data.isType) {
          json = {
            store_id: this.data.store_id,
            goods_id: this.data.goods_id,
            category_pid: this.data.selectedOptions3[0],
            category_cid: this.data.selectedOptions3[1],
            title: this.data.cargon_data.title,
            thumb: this.data.cargon_data.thumb,
            thumb_url: newArr2,
            // good_content: newArr,
            cost_price: this.data.cargon_data.cost_price,
            market_price: this.data.cargon_data.market_price,
            price: this.data.cargon_data.price,
            sku: this.data.cargon_data.sku,
            stock: this.data.cargon_data.stock,
            goods_sn:this.data.cargon_data.goods_sn,
            product_sn:this.data.cargon_data.product_sn,
          };
        }
      } else if (this.data.store == 'supplier') {
        json = {
          title: this.data.cargon_data.title,
          thumb: this.data.cargon_data.thumb,
          thumb_url: newArr2,
          good_content: newArr,
          cost_price: this.data.cargon_data.cost_price,
          market_price: this.data.cargon_data.market_price,
          price: this.data.cargon_data.price,
          sku: this.data.cargon_data.sku,
          stock: this.data.cargon_data.stock,
          content_text: this.data.shopText
        };
        if (!this.data.isType) {
          json = {
            title: this.data.cargon_data.title,
            thumb: this.data.cargon_data.thumb,
            thumb_url: newArr2,
            cost_price: this.data.cargon_data.cost_price,
            market_price: this.data.cargon_data.market_price,
            price: this.data.cargon_data.price,
            sku: this.data.cargon_data.sku,
            stock: this.data.cargon_data.stock
          };
        }
        if (this.data.selectedOptions3.length < 3) {
          json.category_pid = this.data.selectedOptions3[0];
          json.category_cid = this.data.selectedOptions3[1];
        } else {
          json.category_pid = this.data.selectedOptions3[0];
          json.category_cid = this.data.selectedOptions3[1];
          json.category_tid = this.data.selectedOptions3[2];
        }
      }
      this.triggerEvent('getChildoneData', json, { bubbles: false });
    },
    // 跳转测试
    jumpEvent() {
      this.setData({
        jumpState: true
      });
      for (let item in this.data.cargon_data) {
        if (this.data.cargon_data[item] == '') {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.selectedOptions3.length == 0) {
        this.setData({
          jumpState: false
        });
      }
      if (this.data.imageUrl == '') {
        this.setData({
          jumpState: false
        });
      }
    },
    // 获取数据
    getGoodsJson() {
      let that = this;
      var urlStr = '';
      var json = {};
      if (that.data.store == 'store') {
        urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-detail');
        json = {
          goods_id: that.data.goods_id
        };
      } else if (that.data.store == 'supplier') {
        urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.goods-detail');
        json = {
          goods_id: that.data.goods_id
        };
      }
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let selectedOptions3 = that.data.selectedOptions3;
            if (res.data.category_ids.parentid) {
              selectedOptions3.push(Number(res.data.category_ids.parentid));
            }
            if (res.data.category_ids.childid) {
              selectedOptions3.push(Number(res.data.category_ids.childid));
            }
            if (res.data.category_ids.thirdid) {
              selectedOptions3.push(Number(res.data.category_ids.thirdid));
            }
            that.setData({
              selectedOptions3: selectedOptions3
            });

            if (
              res.data.thumb_url != null &&
              res.data.thumb_url != ''
            ) {
              // let fileList1 = that.data.fileList1
              // let fileList2 = that.data.fileList2

              // for (var i = 0; i < res.data.thumb_url.length; i++) {
              //   fileList1.push({
              //     url: res.data.thumb_url[i]
              //   })
              //   fileList2.push({
              //     url: res.data.thumb
              //   })
              // }
              // that.setData({
              //   fileList4: fileList1,
              //   infoArr: fileList1,
              //   fileList2: fileList2,
              //   upNumMax: res.data.thumb_url.length
              // })
              let fileList3 = that.data.fileList3;
              for (var i = 0; i < res.data.thumb_url.length; i++) {
                fileList3.push({
                  url: res.data.thumb_url[i]
                });
              }
              console.log(that.data);
              console.log(fileList3);
              that.setData({
                fileList3: fileList3,
                infoArr: [],
                // fileList4: fileList3,
                upNumMax: res.data.thumb_url.length
              });
            }
            that.setData({
              'cargon_data.title': res.data.title,
              'cargon_data.sku': res.data.sku,
              'imageUrl': res.data.thumb,
              'cargon_data.thumb': res.data.thumb,
              'cargon_data.price': res.data.price,
              'cargon_data.market_price': res.data.market_price,
              'cargon_data.cost_price': res.data.cost_price,
              'cargon_data.stock': res.data.stock,
              'cargon_data.thumb_url': res.data.thumb_url,
              'cargon_data.goods_sn':res.data.goods_sn,
              'cargon_data.product_sn':res.data.product_sn
            });
            // 初始化分类选择
            that.commodityInit();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) { }
      });
    },
    commodityInit() {
      let selectedOptions3 = this.data.selectedOptions3;
      let sectionOptions = this.data.sectionOptions;


      let arr_2 = [];
      let arr_3 = [];
      let str = '';
      let notCategory = true;
      for (let i = 0; i < sectionOptions.length; i++) {
        if (sectionOptions[i].id == selectedOptions3[0]) {
          this.setData({
            'multiIndex[0]': i
          });
          str += sectionOptions[i].name;
          notCategory = true;
          break;
        }else{
          notCategory = false;
        }
      }
      if(!notCategory){
        wx.showModal({
          title: '',
          showCancel:false,
          content: '该商品原设置的分类不存在或已被删除',
        })
        return
      }
      console.log(this.data.multiIndex, '分类的数组');
      console.log(sectionOptions, '选中的项');

      if (sectionOptions[this.data.multiIndex[0]].children.length > 0) {
        for (let k = 0; k < sectionOptions[this.data.multiIndex[0]].children.length; k++) {
          arr_2.push({
            id: sectionOptions[this.data.multiIndex[0]].children[k].id,
            name: sectionOptions[this.data.multiIndex[0]].children[k].name
          });
        }
      }

      let  _values_2 = arr_2.map(obj=>obj.name);
      this.setData({
        'objectMultiArray[1].values': _values_2
      });
      if (sectionOptions[this.data.multiIndex[0]].children.length > 0) {
        for (let k = 0; k < sectionOptions[this.data.multiIndex[0]].children.length; k++) {
          if (sectionOptions[this.data.multiIndex[0]].children[k].id == selectedOptions3[1]) {
            this.setData({
              'multiIndex[1]': k
            });
            str += '/' + sectionOptions[this.data.multiIndex[0]].children[k].name;
            break;
          }
        }
      }
      if ((sectionOptions[this.data.multiIndex[0]].children && sectionOptions[this.data.multiIndex[0]].children.length >
        0) && (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0)) {
        for (let j = 0; j < sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; j++) {
          arr_3.push({
            id: sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].id,
            name: sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].name
          });
        }
      }

      let  _values_3 = arr_3.map(obj=>obj.name);
      this.setData({
        'objectMultiArray[2].values': _values_3
      });
      if ((sectionOptions[this.data.multiIndex[0]].children && sectionOptions[this.data.multiIndex[0]].children.length >
        0) && (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0)) {
        for (let j = 0; j < sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; j++) {
          if (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].id == selectedOptions3[2]) {
            this.setData({
              'multiIndex[2]': j
            });
            str += '/' + sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].name;
            break;
          }
        }
      }
      this.setData({
        regional: str
      });
    }
  }
});
